<!-- 后台管理 - 医院管理 -->
<template>
  <div id="box" class="back">
    <div style="float: right;" class="mg-10 addBtn">
      <el-button
        type="primary"
        size="medium"
        @click="
          dialogStatus = '新增';
          addUpdateVisible = true;
        "
      >
        添加
      </el-button>
    </div>
    <!-- <div style="clear: both;" /> -->
    <div style="width: 100%;background-color: white;padding: 10px 20px;">
      <span>医院名称：</span>
      <el-input v-model="hospitalForm.hospitalName" placeholder="医院名称" style="width: 20%;margin: 10px 20px 20px 0px;" clearable />
      <span>医院类型：</span>
      <el-select v-model="hospitalForm.hospitalType" placeholder="请选择医院类型" style="width: 20%;margin: 10px 20px 20px 0px;" clearable>
        <el-option v-for="(item, index,) in hospitalTypeList" :key="index" :label="item.label" :value="item.value" />
      </el-select>
      <span>医院级别：</span>
      <el-select v-model="hospitalForm.level" placeholder="请选择医院级别" style="width: 20%;margin: 10px 20px 20px 0px;" clearable>
        <el-option v-for="(item, index,) in levelList" :key="index" :label="item" :value="item" />
      </el-select>
      <el-button size="medium" class="fr" style="margin: 10px 20px 20px 0px;" @click="resetData('hospitalForm')">重置</el-button>
      <el-button size="medium" type="primary" class="fr" style="margin: 10px 20px 20px 0px;" @click="clearPagination();searchs()">查询</el-button>
      <el-table border height="550" :data="tableData">
        <el-table-column label="医院编码" prop="code" align="center" />
        <el-table-column label="医院类型" prop="type" align="center">
          <template slot-scope="data">
            <span v-if="data.row.type=='1'">中心医院</span>
            <span v-if="data.row.type=='2'">基层医院</span>
          </template>
        </el-table-column>
        <el-table-column label="医院名称" prop="name" align="center" />
        <el-table-column label="医院级别" prop="level" align="center" />
        <el-table-column label="附属医院(数量)" prop="hoscount" align="center" />
        <el-table-column label="科室数量" prop="deptcount" align="center" />
        <el-table-column label="角色数量" prop="rolecount" align="center" />
        <el-table-column label="设备数量" prop="devcount" align="center" />
        <el-table-column fixed="right" label="操作" width="150" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="dataEcho(scope.row)">编辑医院</el-button>
            <el-button type="text" size="small" @click="scanHospital(scope.row)">查看医院</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageNum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!--添加新医院-->
    <el-dialog :title="dialogStatus + '医院'" :visible.sync="addUpdateVisible" width="40%" center style="text-align: center;" :before-close="closeaddbase">
      <el-form ref="addbase" :model="send" :rules="addbaserules" label-width="100px" style="width: 100%;border-bottom: 1px solid #F0F0EE;border-top: 1px solid #F0F0EE;">
        <el-form-item label="医院名称" prop="hospitalName"><el-input v-model="send.hospitalName" style="width: 80%;" placeholder="请输入医院名称" /></el-form-item>
        <el-form-item label="医院类型" prop="hospitalType">
          <el-select v-model="send.hospitalType" placeholder="请选择医院类型" clearable style="width: 80%;">
            <el-option v-for="(item, index,) in hospitalTypeList" :key="index" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="医院级别" prop="level">
          <el-select v-model="send.level" placeholder="请选择医院级别" clearable style="width: 80%;">
            <el-option v-for="(item, index,) in levelList" :key="index" :label="item" :value="item" />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="resetForm('addbase')">重置</el-button>
        <el-button size="medium" @click="handleClose('addbase')">取 消</el-button>
        <el-button size="medium" type="primary" @click="saveHospitalFun(send, 'addbase')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getHospitalListNew, savehospital, updatehospital } from '@/api/back-management/account-set/hospitalManage'

export default {
  props: {
    tochild: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      addUpdateVisible: false,
      pageNum: 1, // 初始页
      pageSize: 10, //    每页的数据
      total: 0, //    总数
      hospitalForm: {
        hospitalName: '',
        hospitalType: '',
        level: ''
      },
      tableData: [],
      hospitalMap: [],
      send: {
        id: undefined,
        hospitalName: '',
        hospitalType: '', // (医院类型)
        level: ''
      },
      dialogStatus: '',
      hospitalTypeList: [{ value: '1', label: '中心医院' }, { value: '2', label: '基层医院' }],
      levelList: ['一级丙等医院', '一级乙等医院', '一级甲等医院', '二级丙等医院', '二级乙等医院', '二级甲等医院', '三级丙等医院', '三级乙等医院', '三级甲等医院', '三级特等医院'],
      addbaserules: {
        hospitalName: [{
          required: true,
          message: '请输入医院名称',
          trigger: 'blur'
        }],
        hospitalType: [{
          required: true,
          message: '请选择医院类型',
          trigger: 'change'
        }],
        level: [{
          required: true,
          message: '请选择医院级别'
        }]
      }
    }
  },
  created() {
    // this.searchs()
    console.log('父组件传值-', this.tochild)
  },
  mounted() {
    this.searchs()
  },
  methods: {
    // 分页尺寸改变
    handleSizeChange(size) {
      this.pageSize = size
      this.searchs()
    },
    // 分页换页
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.searchs()
    },
    // 修改回显
    dataEcho(row) {
      this.dialogStatus = '修改'
      this.addUpdateVisible = true
      this.$nextTick(() => {
        this.send = {
          hospitalName: row.name,
          hospitalType: row.type,
          level: row.level,
          id: row.id
        }
      })
    },
    // 查看医院
    scanHospital(row) {
      if (row.type === '1') {
        row.toPage = 'center'
      }
      if (row.type === '2') {
        row.toPage = 'basic'
      }
      row.childTitle = row.name
      row.data = {
        hospitalId: row.id.toString(),
        regionId: row.regionId.toString()
      }
      this.$emit('child2Far', row)
    },
    // 重置分页信息
    clearPagination() {
      this.pageNum = 1
      this.pageSize = 10
      this.total = 0
    },
    resetData(name) {
      Object.assign(this.$data[name], this.$options.data()[name])
    },
    searchs() {
      let roleKey = ''
      let keyNum = ''
      if (this.$store.getters.sysRole.includes('超级管理员')) {
        roleKey = 'regionId'
        keyNum = this.tochild.data.regionId
      } else if (this.$store.getters.sysRole.includes('医院管理员')) {
        roleKey = 'hospitalId'
        keyNum = this.$store.getters.hospitalId
      }
      const data = {
        token: this.$store.getters.token,
        type: this.hospitalForm.hospitalType,
        level: this.hospitalForm.level,
        hospitalName: this.hospitalForm.hospitalName,
        [roleKey]: keyNum,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      getHospitalListNew(data)
        .then(res => {
          this.tableData = res.data.list
          this.total = res.data.total
        })
        .catch(err => {
          console.log(err)
        })
    },
    saveHospitalFun(send, formname) {
      this.$refs[formname].validate(valid => {
        if (valid) {
          const data = {
            hospitalName: send.hospitalName,
            hospitalType: send.hospitalType,
            level: send.level,
            parentId: 'root', // （写死）
            token: this.$store.getters.token,
            regionId: this.tochild.data.regionId
          }
          // 获取新增 或 修改的接口
          const saveHospitalApi = this.dialogStatus === '新增' ? savehospital : updatehospital
          if (this.dialogStatus === '新增') {
            data.isAffiliated = false
          } else if (this.dialogStatus === '修改') {
            data.parentName = '组织机构树'
            data.hospitalId = send.id
          } else {
            this.$message.error('保存失败,请刷新页面后重试! ')
          }

          saveHospitalApi(data).then(res => {
            // 存在id则认为保存成功
            console.log(res)
            if (JSON.stringify(res).indexOf('云接口') === -1 && res !== undefined) {
              this.addUpdateVisible = false
              this.$message({
                message: this.dialogStatus + '成功',
                type: 'success'
              })
              this.resetForm(formname)
              this.searchs()
            }
          })
        } else {
          return false
        }
      })
    },
    handleClose(formName) {
      this.addUpdateVisible = false
      this.resetForm(formName)
      Object.assign(this.$data.send, this.$options.data().send)
    },
    closeaddbase() {
      this.$nextTick(() => {
        this.handleClose('addbase')
      })
      this.resetForm('addbase')
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped>
.addBtn{
      position: relative;
      top: -80px;
    }
</style>
